//
//  OMDeliveryAddressTableCell.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2016/12/23.
//  Copyright © 2016年 Kamfat. All rights reserved.
//

#import "OMDeliveryAddressTableCell.h"
#import "OMDeliveryInfo.h"
@interface OMDeliveryAddressTableCell ()
@property (nonatomic, strong) UIView *separatorLine;
@property (nonatomic, strong) UIButton *checkBoxBtn;
@property (nonatomic, strong) UIButton *editBtn;
@property (nonatomic, strong) UIButton *deleteBtn;
@end
@implementation OMDeliveryAddressTableCell

- (void)configureCellWithObj:(OMDeliveryInfo *)info
{
    self.nameLabel.text = info.consignee;
    self.phoneLabel.text = info.phone;
    self.addressLabel.text = [NSString stringWithFormat:@"%@ %@",info.areaName,info.address];
    self.om_default = info.isDefault;
}

- (void)configure
{
    [super configure];
    [self.contentView addSubview:self.separatorLine];
    [self.contentView addSubview:self.editBtn];
    [self.contentView addSubview:self.deleteBtn];
    [self.contentView addSubview:self.checkBoxBtn];
    self.backgroundColor = SM_whiteColor;
    self.contentView.backgroundColor = SM_whiteColor;

    self.nameLabel.text = @"梁先生";
    self.phoneLabel.text = @"13333333333";
    self.addressLabel.text = @"嘉信城市花园1期 22栋325A";

    [self.separatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containView.mas_bottom).offset(SM_SIZE(0));
        make.left.equalTo(@(SM_SIZE(20)));
        make.right.equalTo(@(SM_SIZE(-20)));
        make.height.equalTo(@0.5f);
    }];

    [self.bottomBar mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
        make.height.equalTo(@(SM_SIZE(15)));
        make.top.equalTo(self.checkBoxBtn.mas_bottom).offset(SM_SIZE(20));
    }];

    [self.checkBoxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(30)));
        //make.bottom.equalTo(@(SM_SIZE(-10)));
        make.top.equalTo(self.separatorLine.mas_bottom).offset(SM_SIZE(20));
    }];

    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(SM_SIZE(-30)));
        make.centerY.equalTo(self.checkBoxBtn.mas_centerY);
    }];

    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.deleteBtn.mas_left).offset(SM_SIZE(-30));
        make.centerY.equalTo(self.checkBoxBtn.mas_centerY);
    }];

}

#pragma mark - Getters

- (UIView *)separatorLine
{
    if (!_separatorLine) {
        _separatorLine = [UIView new];
        _separatorLine.backgroundColor = HEXCOLOR(@"eaeaea");;
    }
    return _separatorLine;
}

- (UIButton *)editBtn
{
    if (!_editBtn) {
        _editBtn = [UIButton new];
        [_editBtn setTitle:@"编辑" forState:UIControlStateNormal];
        [_editBtn setImage:[UIImage imageNamed:@"btn_address_edit_temp"] forState:UIControlStateNormal];
        [_editBtn setTitleColor:HEXCOLOR(@"#606061") forState:UIControlStateNormal];
        [_editBtn.titleLabel setFont:SM_CFont(28)];
        _editBtn.contentEdgeInsets = UIEdgeInsetsMake(SM_SIZE(10), SM_SIZE(10), SM_SIZE(10), SM_SIZE(10));
        [_editBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _editBtn;
}

- (UIButton *)deleteBtn
{
    if (!_deleteBtn) {
        _deleteBtn = [UIButton new];
        [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
        [_deleteBtn setImage:[UIImage imageNamed:@"btn_address_delete_temp"] forState:UIControlStateNormal];
        [_deleteBtn setTitleColor:HEXCOLOR(@"#606061") forState:UIControlStateNormal];
        [_deleteBtn.titleLabel setFont:SM_CFont(28)];
        _deleteBtn.contentEdgeInsets = UIEdgeInsetsMake(SM_SIZE(10), SM_SIZE(10), SM_SIZE(10), SM_SIZE(10));
        [_deleteBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

- (UIButton *)checkBoxBtn
{
    if (!_checkBoxBtn) {
        _checkBoxBtn = [UIButton new];
        [_checkBoxBtn setImage:[UIImage imageNamed:@"btn_shoppingcart_s"]  forState:UIControlStateNormal];
        [_checkBoxBtn setImage:[UIImage imageNamed:@"address_selected_19x19_"] forState:UIControlStateSelected];
        [_checkBoxBtn setTitle:@"设为默认" forState:UIControlStateNormal];
        [_checkBoxBtn setTitle:@"默认地址" forState:UIControlStateSelected];
        [_checkBoxBtn setTitleColor:SM_darkGrayColor forState:UIControlStateSelected];
        [_checkBoxBtn setTitleColor:SM_lightGrayColor forState:UIControlStateNormal];
        [_checkBoxBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_checkBoxBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, SM_SIZE(10), 0, SM_SIZE(-10))];
        [_checkBoxBtn.titleLabel setFont:SM_CFont(28)];
    }
    return _checkBoxBtn;
}



- (void)setOm_default:(BOOL)om_default
{
    _om_default = om_default;
    self.checkBoxBtn.selected = _om_default;
}

- (void)buttonClick:(UIButton *)button
{
    if (button == _checkBoxBtn) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectDefaultBtnAtIndexPath:)]) {
            [self.delegate didSelectDefaultBtnAtIndexPath:[(UITableView *)self.superview.superview indexPathForCell:self]];
        }
    }else if (button == _deleteBtn) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(didClickDeleteButtonAtIndexPath:)]) {
            [self.delegate didClickDeleteButtonAtIndexPath:[(UITableView *)self.superview.superview indexPathForCell:self]];
        }
    }else if (button == _editBtn) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(didClickEditButtonAtIndexPath:)]) {
            [self.delegate didClickEditButtonAtIndexPath:[(UITableView *)self.superview.superview indexPathForCell:self]];
        }
    }

}


+ (CGFloat)cellHeight
{
    return SM_SIZE(220+15);
}
@end
